package com.swak.metrics.health;

import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.swak.App;
import com.swak.Constants;
import com.swak.OS;
import com.swak.registry.RegistryService;
import com.swak.registry.URL;
import com.swak.utils.Maps;

/**
 * 健康上报服务
 * 
 * @author lifeng
 */
public class HealthRegister implements InitializingBean {

	@Autowired(required = false)
	private RegistryService registryService;
	@Autowired
	private App app;
	protected URL url;

	@Override
	public void afterPropertiesSet() throws Exception {
		Map<String, String> parameters = Maps.newHashMap();
		parameters.put(Constants.Application_KEY, app.getName());
		parameters.put(Constants.Service_Key, Constants.Registry_Health);
		parameters.put(Constants.Server_Key, app.getId());
		this.url = new URL(Constants.Registry_Monitor_Key, OS.ip(), 0, Constants.Registry_Health, parameters);
	}

	public void start() {
		if (this.registryService != null && this.url != null) {
			this.registryService.register(this.url);
		}
	}
}
